// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env:"tfjkp-0ge80vu9ea710148"
})


// 云函数入口函数
exports.main = async (event, context) => {
    //更新当前用户下，绩点总量

    let { OPENID, APPID, UNIONID } = await cloud.getWXContext()

    const db = cloud.database();
    const _ = db.command;
    
    db.collection('enerayBall').where({_id:event._id}).update({
        data:{
            _isFetch:true
        }
    })

    return new Promise((resolve, reject) => {
        db.collection('userInfo').where({openid:OPENID}).get().then(res => {
            
            db.collection('userInfo').where({openid:OPENID}).update({
               
                data:{
                    _eneray:_.inc(event._eneray) 
                }
            }).then(resp => {
                resolve(res.data._eneray + event._eneray)
            })
    
        })
    }) 
   
    
}